<template>
  <el-card>
    <bread data1="商品管理" data2="商品分类"></bread>
    <el-button type="success" @click="add">添加分类</el-button>
    <el-table :data="form" style="width:100%" 
    row-key="cat_id"

    :tree-props="{children: 'children',  hasChildren: 'hasChildren'}"
    lazy
    :load="load"
    >
      <el-table-column prop="cat_name" label="分类名称" > </el-table-column>
      <el-table-column prop="name" label="级别" > 
        <template v-slot='scope'>
          {{ scope.row.cat_level+1+'级' }}
        </template>
      </el-table-column>
      <el-table-column prop="address" label="是否有效">
        {{ '是' }}
      </el-table-column>
       <el-table-column label="操作" prop="desc">
            <template v-slot="scope">
              <el-row>
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  size="mini"
                  plain
                  @click="editorUser(scope.row)"
                ></el-button>
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  size="mini"
                  plain
                  @click="deluser(scope.row.cat_id)"
                ></el-button>
              </el-row>
            </template>
          </el-table-column>
    </el-table>
    <!-- 对话框 -->
    <el-dialog title="添加商品分类" :visible.sync="dialogFormVisible">
      <el-form :model="addcates">
        <el-form-item label="商品名称" label-width="70px">
          <el-input v-model="addcates.cat_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品层级">
          <el-cascader
            v-model="value"
            :options="categories"
            :props="prpoes"
            @change="handerchange"
             clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      form: [],
      prpoes: {
        expandTrigger: "hover",
        checkStrictly: true,
        value: "cat_id",
        label: "cat_name",
        children: "children"
      },
      categories: [],
      value: [],
      pagenum: 1,
      pagesize: 10,
      addcates:{
      cat_name:''
      }
    };
  },
  created() {
    this.getGoodsCata();
  },
  methods: {
    async getGoodsCata() {
      const res = await this.$http.get(`categories?type=3&pagenum=${this.pagenum}&pagesize=${this.pagesize}`);
      this.form = res.data.data.result;
      this.categories = res.data.data.result
      this.form.forEach( item =>{
        item.hasChildren = true
        item.children.forEach(item2 =>{
          item2.hasChildren = true
        })
      })
    },
    add() {
      this.dialogFormVisible = true;
    },
    handerchange() {},
    editorUser(){},
    async deluser(cat_id){
     const res = await this.$http.delete(`categories/${cat_id}`)
     const { meta:{msg,status}} = res.data
     if(  status === 200) {
       this.$message.success(msg)
     }
     this.getGoodsCata()
    },
   load(tree, treeNode, resolve) {
        setTimeout(() => {
          resolve(tree.children)
        }, 500)
   },
   async addCate(){
     if( this.value.length === 2){
    const res = await this.$http.post('categories',{
      cat_pid:this.value[1],
      cat_name:this.addcates.cat_name,
      cat_level:this.value.length
    })
    }else if( this.value.length === 1){
       const res = await this.$http.post('categories',{
      cat_pid:this.value[0],
      cat_name:this.addcates.cat_name,
      cat_level:this.value.length
    })
    }else if( this.value.length === 0 ){
      const res = await this.$http.post('categories',{
      cat_pid:0,
      cat_name:this.addcates.cat_name,
      cat_level:this.value.length
    })
    }
     this.dialogFormVisible = false
    this.getGoodsCata()

   }
  }
};
</script>

<style lang="scss" scoped>
.el-button {
  margin: 10px 0;
}
</style>
